import javax.swing.*;
import java.awt.*;
import java.sql.*;

public class Util {
    public static Image getImage(String path) {
        return new ImageIcon(Util.class.getResource(path)).getImage();
    }

    public static Icon getIcon(String path) {
        return new ImageIcon(Util.class.getResource(path));
    }

    /**
     * 创建数据库连接
     */
    public Connection getConnection() {
        Connection con = null;
        try {
            // 1. 加载驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            // 2. 创建连接
            con = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/j189?userSSL=true&useUnicode=true&characterEncoding=UTF8&serverTimezone=GMT",
                    "root", "yks957");

        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }
        return con;
    }

    /**
     * 根据用户名和密码查询用户对象
     *
     * @param username
     * @param pwd
     * @return
     */
    public User getByUsernameAndPwd(String username, String pwd) {
        Connection con = getConnection();
        PreparedStatement statement = null;
        ResultSet res = null;
        User user = null;
        String sql = "select * from t_user where u_name=?and u_password=?";
        try {
            statement = con.prepareStatement(sql);
            statement.setObject(1, username);
            statement.setObject(2, pwd);
            res = statement.executeQuery();
            if (res.next()) {
                user = new User();
                user.setId(res.getInt("u_id"));
                user.setName(res.getString("u_name"));
                user.setPwd(res.getString("u_password"));
                user.setMoney(res.getInt("u_money"));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close(con, statement, res);
        }
        return user;
    }

    /**
     * 关闭连接
     *
     * @param con
     * @param statement
     * @param res
     */
    public void close(Connection con, Statement statement, ResultSet res) {
        if (res != null) {
            try {
                res.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (statement != null) {
            try {
                statement.close();
                con.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }

    public void close(Connection con, Statement statement) {
        if (statement != null) {
            try {
                statement.close();
                con.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }

    /**
     * 根据id查询
     *
     * @param id
     */
    public User getById(int id) {
        Connection con = getConnection();
        PreparedStatement statement = null;
        ResultSet rs = null;
        User user = null;
        String sql = "select * from t_user where u_id=?";
        try {
            statement = con.prepareStatement(sql);
            statement.setObject(1, id);
            rs = statement.executeQuery();
            if (rs.next()) {
                user = new User();
                user.setId(rs.getInt("u_id"));
                user.setName(rs.getString("u_name"));
                user.setPwd(rs.getString("u_password"));
                user.setMoney(rs.getInt("u_money"));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close(con, statement, rs);
        }
        return user;
    }

    /**
     * 根据用户名查询用户对象
     *
     * @param username
     * @return
     */
    public User getByUsername(String username) {
        Connection con = getConnection();
        PreparedStatement statement = null;
        ResultSet rs = null;
        User user = null;
        String sql = "select * from t_user where u_name=?";
        try {
            statement = con.prepareStatement(sql);
            statement.setObject(1, username);
            rs = statement.executeQuery();
            if (rs.next()) {
                user = new User();
                user.setId(rs.getInt("u_id"));
                user.setName(rs.getString("u_name"));
                user.setPwd(rs.getString("u_password"));
                user.setMoney(rs.getInt("u_money"));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close(con, statement, rs);
        }
        return user;
    }
    public void update(User user){
        // 1. 创建连接
        Connection con = getConnection();
        PreparedStatement statement = null;
        String sql = "update t_user set u_pwd=?,u_blance=? where u_id=?";
        try {
            // 2. 获取执行SQL语句对象
            statement = con.prepareStatement(sql);
            statement.setObject(1,user.getPwd());
            statement.setObject(2,user.getMoney());
            statement.setObject(3,user.getId());
            // 3. 执行SQL语句
            int row = statement.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            // 4. 关闭连接
            close(con,statement);
        }
    }

    public void update(User user1, User user2) {
        Connection con = getConnection();
        PreparedStatement statement = null;
        try {
            con.setAutoCommit(false);
            String sql = "update t_user set u_password=?,u_money=? where u_id=?";
            statement = con.prepareStatement(sql);
            statement.setObject(1, user1.getPwd());
            statement.setObject(2, user1.getMoney());
            statement.setObject(3, user1.getId());
            statement.executeUpdate();
            statement = con.prepareStatement(sql);
            statement.setObject(1, user2.getPwd());
            statement.setObject(2, user2.getMoney());
            statement.setObject(3, user2.getId());
            statement.executeUpdate();
            con.commit();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            try {
                con.rollback();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        } finally {
            close(con, statement);
        }
    }
}
